package cn.xyh.service.impl;


import cn.xyh.dao.PermissionWriteMapper;
import cn.xyh.exception.PermissionException;
import cn.xyh.model.Permission;
import cn.xyh.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionWriteMapper permissionWriteMapper;


    /**
     * 新增
     */
    @Override
    public Integer insertPermission(Permission entity) throws PermissionException {

        Integer result = 0;

        try {
            result = permissionWriteMapper.insertPermission(entity);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除多个
     */
    @Override
    public Integer delPermissionByIds(String entityIds) throws PermissionException {
        Integer result = 0;
        try {
            String[] _entityIds = entityIds.split(",");
            result = permissionWriteMapper.delPermissionByIds(_entityIds);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除
     */
    @Override
    public Integer delPermission(long id) throws PermissionException {
        Integer result = 0;
        try {

            result = permissionWriteMapper.delPermission(id);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }


    /**
     * 修改
     */
    @Override
    public Integer updatePermission(Permission entity) throws PermissionException {
        int result = 0;

        try {

            result = permissionWriteMapper.updatePermission(entity);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据ID查找对象
     */
    @Override
    public Permission getPermissionById(long permissionId) throws PermissionException {

        Permission entity = null;
        try {
            entity = permissionWriteMapper.getPermissionById(permissionId);


        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据Code查找对象
     */
    @Override
    public Permission getPermissionByCode(String code) throws PermissionException {

        Permission entity = null;
        try {
            entity = permissionWriteMapper.getPermissionByCode(code);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据name查找对象
     */
    @Override
    public Permission getPermissionByName(String name) throws PermissionException {

        Permission entity = null;
        try {
            entity = permissionWriteMapper.getPermissionByName(name);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return entity;
    }

    /**
     * 根据条件查找列表总数
     */
    @Override
    public Integer getPermissionCount(Permission entity) throws PermissionException {
        int result = 0;
        try {
            result = permissionWriteMapper.getPermissionCount(entity);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return result;
    }

    /**
     * 根据条件查找列表
     *
     * @param entity      实体对象
     * @param startRownum 开始条数
     * @param endRownum   结束条数
     * @param sortField   排序字段
     * @param sortOrder   正序/倒序
     */
    @Override
    public List<Permission> getPermissionList(Permission entity) throws PermissionException {


        List<Permission> list = null;

        try {
            list = permissionWriteMapper.getPermissionList(entity);

        } catch (Exception e) {
            throw new PermissionException(this.getClass().getName() + "出错了，" + e.getMessage(), e);
        }
        return list;
    }

    @Override
    public Set<String> getPermsByySetName(List<Integer> roleset) {
        Set<String> permsset = permissionWriteMapper.getPermsByySetName(roleset);
        return permsset;
    }
}

